package com.example.adminbackend.controller;

import com.example.adminbackend.client.AdServiceClient;
import com.example.adminbackend.entity.Ad;
import com.example.adminbackend.entity.AdCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdServiceClient adServiceClient;

    @GetMapping("/ads")
    public String adList(Model model) {
        List<Ad> ads = adServiceClient.findAll();
        model.addAttribute("ads", ads);
        return "ad-list";
    }

    @GetMapping("/ads/add")
    public String addAdForm(Model model) {
        List<AdCategory> categories = adServiceClient.findAllCategories();
        model.addAttribute("ad", new Ad());
        model.addAttribute("categories", categories);
        return "ad-add";
    }

    @GetMapping("/ads/edit/{id}")
    public String editAdForm(@PathVariable Integer id, Model model) {
        Ad ad = adServiceClient.findById(id);
        List<AdCategory> categories = adServiceClient.findAllCategories();
        model.addAttribute("ad", ad);
        model.addAttribute("categories", categories);
        return "ad-edit";
    }

    @PostMapping("/ads/save")
    public String saveAd(Ad ad, @RequestParam(value = "imageFile", required = false) MultipartFile imageFile) {
        if (imageFile != null && !imageFile.isEmpty()) {
            String imagePath = adServiceClient.uploadImage(imageFile);
            ad.setImageUrl(imagePath);
        }
        if (ad.getId() == null) {
            adServiceClient.save(ad);
        } else {
            adServiceClient.update(ad.getId(), ad);
        }
        return "redirect:/admin/ads";
    }

    @GetMapping("/ads/delete/{id}")
    public String deleteAd(@PathVariable Integer id) {
        adServiceClient.delete(id);
        return "redirect:/admin/ads";
    }

    @GetMapping("/categories")
    public String categoryList(Model model) {
        List<AdCategory> categories = adServiceClient.findAllCategories();
        model.addAttribute("categories", categories);
        return "category-list";
    }

    @GetMapping("/categories/add")
    public String addCategoryForm(Model model) {
        model.addAttribute("category", new AdCategory());
        return "category-add";
    }

    @GetMapping("/categories/edit/{id}")
    public String editCategoryForm(@PathVariable Integer id, Model model) {
        AdCategory category = adServiceClient.findCategoryById(id);
        model.addAttribute("category", category);
        return "category-edit";
    }

    @PostMapping("/categories/save")
    public String saveCategory(AdCategory category) {
        if (category.getId() == null) {
            adServiceClient.saveCategory(category);
        } else {
            adServiceClient.updateCategory(category.getId(), category);
        }
        return "redirect:/admin/categories";
    }

    @GetMapping("/categories/delete/{id}")
    public String deleteCategory(@PathVariable Integer id) {
        adServiceClient.deleteCategory(id);
        return "redirect:/admin/categories";
    }
}